import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Article } from "src/entities/article.entity";
import { Classify } from "src/entities/classify.entity";
import { User } from "src/entities/user.entity";
import { ArticleInput, ArticleSortInput, ArticleWhereInput, PageInput } from "src/graphql";
import { ArticleService } from "src/services/article/article.service";
import { ClassifyService } from "src/services/classify/classify.service";
import { UserService } from "src/services/user/user.service";

@Resolver('Article')
export class ArticleResolver {
  constructor(
    private readonly articleService: ArticleService,
    private readonly userService: UserService,
    private readonly classifyService: ClassifyService,
  ) {}

  @Query('article')
  async getArticleById(@Args() id: number) {
    return await this.articleService.findOne(id);
  }

  @Query('articles')
  async getArticles(@Args('page') page: PageInput,@Args('sort') sort: ArticleSortInput,@Args('where') where:ArticleWhereInput) {
    return await this.articleService.getPagination(page,sort,where);
  }

  @Mutation()
  async addArticle(@Args('article') article:ArticleInput) {
    return await this.articleService.addOne(<Article>article);
  }

  @Mutation()
  async removeArticle(@Args() id: number) {
    return await this.articleService.removeOne(id);
  }

  @Mutation()
  async modifyArticle(@Args('id') id: number, @Args('article') article:ArticleInput) {
    return await this.articleService.modifyOne(id,<Article>article);
  }

  @ResolveProperty(() => User,{
    name:'_user'
  })
  async user(@Parent() article){
    const { user_id } = article;
    return await this.userService.findOne(user_id);
  }

  @ResolveProperty(() => Classify,{
    name:'_classify'
  })
  async classify(@Parent() classify){
    const { classify_id } = classify;
    return await this.classifyService.findOne(classify_id);
  }
}